package com.minxi.jsp.controller;

import com.minxi.jsp.javabean.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet(name = "Detail1Servlet",urlPatterns = "/detail1")
public class Detail1Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);
            String sql = "select id,user_name,user_pwd,user_phone,user_sex from user_info where id = ?;";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1,Long.parseLong(id));
            ResultSet resultSet = preparedStatement.executeQuery();
            UserInfo userInfo = new UserInfo();
            while (resultSet.next()){
                userInfo.setId(resultSet.getLong("id"));
                userInfo.setUser_name(resultSet.getString("user_name"));
                userInfo.setUser_pwd(resultSet.getString("user_pwd"));
                userInfo.setUser_phone(resultSet.getString("user_phone"));
                userInfo.setUser_gender(resultSet.getString("user_sex"));
            }
            //把list数据传到页面上 HttpServletRequest
            HttpSession session1 = req.getSession();
            //查询返回的数据设置到当前的会话域
            session1.setAttribute("userInfo",userInfo);
            //跳转页面
            resp.sendRedirect("jsp/detail1.jsp");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
